package tests;

import global.*;
import heap.*;
import iterator.*;

import java.io.*;
import java.util.Random;

public class MYTest {
	private static Random rand = new Random((long)1000);

   public static void main (String argv[]) {

     System.out.print ("Hello World!\n");

// test new Vector100Dtype type
     global.Vector100Dtype vd = new global.Vector100Dtype();
     for(short n = 0; n < 100; n++)
     {
    	 vd.setIndex(n, n);
     }
     
     for(int i =0; i<100; i++)
     {
    	 System.out.format("%dth member is %d\n", i, vd.getIndex(i));
     }
     
// test Distance function
     global.Vector100Dtype v1 = new global.Vector100Dtype();
     global.Vector100Dtype v2 = new global.Vector100Dtype();
     for (int i=0; i<100; i++)
     {
    	 v1.setIndex(i, (short)1);
    	 v2.setIndex(i, (short)2);
     }

     System.out.format("Expected Distance is 10, actual distance is %f\n", 
    		 Vector100Dtype.Distance(v1, v2));

// test maximum distance
     for (int i=0; i<100; i++)
     {
    	 v1.setIndex(i, (short)-10000);
    	 v2.setIndex(i, (short)10000);
     }
     System.out.format("Maximum distance is %f\n", 
    		 Vector100Dtype.Distance(v1, v2));

// test convert
     // first, write values into a byte array using convert
     byte[] B = new byte[400];
     try 
     {
		Convert.setVector100DValue(vd, 25, B);
     }
     catch (IOException e) {
		System.err.println(e.getMessage());
     }
     // now, try to read the vector back from the byte array
     Vector100Dtype vresult;
     try
     {
    	 vresult = Convert.getVector100DValue(25, B);
    	 // output the vector values
    	 System.out.println("Result of getVector");
    	 System.out.println(vresult.toString());
     }
     catch (IOException e)
     {
 		System.err.println(e.getMessage());
     }

// tuple tests
     Tuple tuple = new Tuple();
     // add a header
     AttrType[] types = {new AttrType(AttrType.attrInteger), 
    		 new AttrType(AttrType.attrReal),
    		 new AttrType(AttrType.attrVector100D),
    		 new AttrType(AttrType.attrString)};
     try 
     {
    	// add field values
		tuple.setHdr((short)4, types, new short[] {(short)4});
		tuple.setIntFld(1, 250);
		tuple.setStrFld(4, "test");
		tuple.set100DVectFld(3, vd);
		tuple.setFloFld(2, (float) 12.75);
		// verify that the vector value can be read back
		vresult = tuple.get100DVectFld(3);
   	 	System.out.println("Reading vector from tuple:");
   	 	System.out.println(vresult.toString());
   	 	System.out.println("Printing tuple:");
   	 	tuple.print(types);
     } 
     catch (InvalidTypeException e) {
    	 System.err.println(e.getMessage());
     } catch (InvalidTupleSizeException e) {
    	 System.err.println(e.getMessage());
     } catch (IOException e) {
    	 System.err.println(e.getMessage());
     } catch (FieldNumberOutOfBoundException e) {
    	 System.err.println(e.getMessage());
     }
     
     MYSortTest sorttest = new MYSortTest();
     sorttest.runTests();
     
     MYJoinTest jointest = new MYJoinTest();
     jointest.runTests();
     
     
     Runtime.getRuntime().exit(0);
   }

}
